package cn.bdqn.service.impl;

import cn.bdqn.domain.Admin;
import cn.bdqn.domain.BaseRegister;
import cn.bdqn.mapper.AdminMapper;
import cn.bdqn.service.AdminService;
import cn.bdqn.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * 管理员业务接口实现类
 * @author dkj
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;


    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteByPrimaryKey(Integer id) {

        //根据id删除用户
        adminMapper.deleteByPrimaryKey(id);
    }

    /**
     * 添加用户
     * @param record
     */
    @Override
    public void insert(Admin record) {

        //MD5加密
        record.setAdminPwd(MD5Util.encode(record.getAdminPwd()));
        //设置token
        record.setToken(UUID.randomUUID().toString());
        //新增
        adminMapper.insert(record);
    }

    /**
     *
     * @param record
     */
    @Override
    public void insertSelective(Admin record) {

        //MD5加密
        record.setAdminPwd(MD5Util.encode(record.getAdminPwd()));
        //设置token
        record.setToken(UUID.randomUUID().toString());
        //选择添加
        adminMapper.insertSelective(record);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Admin queryByPrimaryKey(Integer id) {

        //根据id查询用户
        return adminMapper.selectByPrimaryKey(id);
    }


    /**
     * 选择更新
     * @param record
     */
    @Override
    public void updateByPrimaryKeySelective(Admin record) {

        //设置密码加密
        record.setAdminPwd(MD5Util.encode(record.getAdminPwd()));
        //设置token
        record.setToken(UUID.randomUUID().toString());
        //更新用户
        adminMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 更新全部
     * @param record
     */
    @Override
    public void updateByPrimaryKey(Admin record) {

        //设置密码加密
        record.setAdminPwd(MD5Util.encode(record.getAdminPwd()));
        //设置token
        record.setToken(UUID.randomUUID().toString());
        //更新全部信息
        adminMapper.updateByPrimaryKey(record);
    }


    /**
     * 登录
     * @param phone
     * @param pwd
     * @return
     */
    @Override
    public Admin queryByPhoneAndPwd(String phone, String pwd) {

        //根据手机号和密码查询
        return adminMapper.selectByPhoneAndPwd(phone,MD5Util.encode(pwd));
    }

    /**
     * 判断手机号是否存在
     * @param phone
     * @return true 代表存在   false代表不存在
     */
    @Override
    public boolean queryByPhone(String phone) {
        //判断手机号是否存在
        Integer count = adminMapper.selectByPhone(phone);
        if (count>0){
            return true;
        }
        return false;

    }

    /**
     * 判断邮箱是否存在
     * @param email
     * @return true 代表存在   false代表不存在
     */
    @Override
    public boolean queryByEmail(String email) {

        //判断邮箱是否存在
        Integer count = adminMapper.selectByEmail(email);
        if (count>0){
            return true;
        }
        return false;
    }

    /**
     * 根据管理员账号id和原密码修改新的密码
     * @param id
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @Override
    public Admin updateByIdAndOldPwd(Integer id, String oldPwd, String newPwd) {
        //查询当前对象

        Admin admin = adminMapper.selectByPrimaryKey(id);

        //判断是否密码相等
        if (admin.getAdminPwd().equals(MD5Util.encode(oldPwd))){
            //修改新密码
            admin.setAdminPwd(MD5Util.encode(newPwd));
//            设置token
            admin.setToken(UUID.randomUUID().toString());
            //修改
            adminMapper.updateByPrimaryKeySelective(admin);
            //返回
            return admin;
        }
        //如果不一样，返回null
        return null;
    }


}
